//
//  TTCoverView.swift
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/28.
//  Copyright © 2016年 陶路路. All rights reserved.
//

import UIKit
import pop

class TTCoverView: UIView {
   


    var dataArray:[TTCoverViewModel] = [TTCoverViewModel]()
    
    var buttonArray:[TTCoverButton] = [TTCoverButton]()
    
    lazy var bgImage: UIImageView = {

       let imv = UIImageView(image: self.getBgImageView().applyLightEffect())
        return imv
        
    }()
    
    var logoImage:UIImageView = UIImageView(image: UIImage(named: "compose_slogan"))
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI(){
        
        backgroundColor = UIColor.whiteColor()
        dataArray = getButtonInfo()
        addSubview(bgImage)
        addSubview(logoImage)
        addButton()
        logoImage.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self)
            make.top.equalTo(self).offset(100)
        }
        
        bgImage.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(self).offset(UIEdgeInsetsZero)
        }
        
        buttonAnimation(true)
    }
    
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
        buttonAnimation(false)
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(0.5 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), { () -> Void in
            
            self.removeFromSuperview()

        })
    }
    
    func addButton(){
        
        let  wid = CGFloat(80)
        let margin = (SCREENW - 3 * wid)/4
        let hig = CGFloat(110)
        
        for (i,value) in dataArray.enumerate(){
            
            let button : TTCoverButton = TTCoverButton()
            
            let  col = CGFloat(i / 3)
            let  rol = CGFloat(i % 3)
            
            button.frame = CGRectMake(rol * wid + margin*(rol+1),col*(hig+margin) + SCREENH, wid, hig)
            button.setImage(UIImage(named: value.icon!), forState: UIControlState.Normal)
            button.setTitle(value.title, forState: UIControlState.Normal)
            button.buttonVC = self.dataArray[i].nextvc
            addSubview(button)
            buttonArray.append(button)
        }
        
    }
    
//    获取文件
    
    func getButtonInfo()->[TTCoverViewModel]{
        
        let path = NSBundle.mainBundle().pathForResource("compose.plist", ofType: nil)
        
        let tempArray = NSArray(contentsOfFile: path!)
        
        var mutableArray :[TTCoverViewModel] = [TTCoverViewModel]()
        
        for value in tempArray!{
            
            let model:TTCoverViewModel = TTCoverViewModel(dict: value as! [String : AnyObject])
            
            mutableArray.append(model)
        }
        
        return mutableArray
    }
    
//     截图
    func getBgImageView() -> UIImage{
        
        let bgWindow:UIWindow = UIApplication.sharedApplication().keyWindow!
        
        UIGraphicsBeginImageContext(bgWindow.bounds.size)
        
        bgWindow.drawViewHierarchyInRect(bgWindow.bounds, afterScreenUpdates: false)
        
        let bgImage = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        return bgImage
    }


}

// mark : 设置动画

extension TTCoverView{

    func buttonAnimation(isLogin:Bool){
        
        let margin : CGFloat = isLogin ? -350 : 350
        
        let array = isLogin ? buttonArray.enumerate() : buttonArray.reverse().enumerate()
        
        for (i,value) in array{
            
            let animation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
            
            animation.toValue = NSValue(CGPoint: CGPoint(x: value.center.x, y: value.center.y + margin))
            
            animation.beginTime = CACurrentMediaTime() + Double(i) * 0.02
            
            animation.springBounciness = 10
            
            animation.springSpeed = 8
            
            value.pop_addAnimation(animation, forKey: nil)
        }
        
    }
}
